var config = require('../../../utils/config.js');
var util = require('../../../utils/util.js');
var API_URL = config.API_URL;
var app = getApp();
var page, msgPage, status, orderBy, loading, news_page, news_loading;
let getData = (that) => {
    return new Promise(function (resolve, reject) {
        if (loading) {
            return
        }
        loading = true;
        let url = API_URL.demand_me;
        let params = { page: page, status: status, order_by: orderBy }
        util.PRL(url, params)
            .then(res => {
                let data = res.data.data.demands;
                let demands;
                console.log(data)
                console.log(res.data);
                if (page == 1) {
                    demands = [];
                } else {
                    demands = that.data.demands;
                }
                demands = demands.concat(data);
                demands = demands.map(item => {
                    item.hidden = true;
                    return item;
                })
                that.setData({
                    demands: demands,
                    count: res.data.data.count
                })
                if (data.length != 0) {
                    page++
                }
                loading = false;
                resolve();
            })
            .catch(res => {
                console.log(res)
                loading = false;
                reject();
            })


    })
}
let getMsg = that => {
    util.PRL(API_URL.demand_msg, { msgPage: msgPage })
        .then(res => {
            that.setData({
                msg: res.data.data
            })
        }).catch(res => {
            console.log(res)
        })
}
Page({
    data: {
        hidden: true,
        new_icon: 'down',
        status_name: '全部',
        status_icon: 'downArrow',
        time_icon: 'default',
        statusHidden: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        let status_name = that.data.status_name;
        status = options.status == 'undefined' ? undefined : options.status;
        switch (status) {
            case '0': status_name = '未完结'; break;
            case '1': status_name = '已完结'; break;
        }
        page = 1;
        my.getSystemInfo({
            success: function (res) {
                let height = res.windowHeight;
                console.log(height);
                height = height - 112;
                that.setData({
                    height: height,
                    status_name: status_name
                })
            },
            fail: function (res) { },
            complete: function (res) { },
        })
        getData(that)
            .then(res => {
                getMsg(that)
            })
    },
    /**
* 生命周期函数--监听页面显示
*/
    onShow: function () {
        if (app.globalData.refresh) {
            console.log('refresh');
            app.globalData.refresh = false;
            page = 1;
            getData(this)
        }
    },
    // 切换状态改变
    changeStatus: function (e) {
        console.log(e);
        status = e.currentTarget.dataset.status;
        let name = e.currentTarget.dataset.name;
        page = 1;
        this.setData({
            statusHidden: true,
            status_name: name,
            status_icon: 'downArrow'
        })
        getData(this);
    },
    // 未完成的切换
    switchStatus: function (e) {
        console.log(e)
        let hidden = !this.data.statusHidden;
        let status_icon;
        if (hidden) {
            status_icon = 'downArrow';
        } else {
            status_icon = 'upArrow';
        }
        this.setData({
            statusHidden: hidden,
            status_icon: status_icon
        })
    },
    // 时间的切换
    time: function (e) {
        let time_icon;
        page = 1;
        if (orderBy != 'asc') {
            orderBy = 'asc';
            time_icon = 'asc'
        } else {
            orderBy = 'desc';
            time_icon = 'desc';
        }
        this.setData({
            time_icon: time_icon
        })
        getData(this);
    },
    // 求购详情
    info: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let demands = that.data.demands;
        app.globalData.demand = demands[index];
        my.navigateTo({
            url: '/pages/demand/info/info',
        })
    },

})